<template>
  <div class="system-container">
    <vab-query-form>
      <el-form :inline="true" :model="queryForm" @submit.native.prevent>
        <el-form-item class="pr8">
          <el-select
            v-model="queryForm.machine"
            clearable
            filterable
            placeholder="机器"
            style="width: 120px"
          >
            <el-option
              v-for="(item, index) in machines"
              :key="index"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="pr8">
          <el-input
            v-model="queryForm.blood_id"
            clearable
            placeholder="血袋号"
            style="width: 120px"
          />
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="queryForm.created_at"
            end-placeholder="结束日期"
            range-separator=" ~ "
            start-placeholder="入库起始日期"
            style="width: 250px"
            type="daterange"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="queryData">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      @row-dblclick="handleDetail"
    >
      <el-table-column
        align="center"
        label="血袋号"
        prop="blood_id"
        width="80"
      />
      <el-table-column
        align="center"
        label="机器"
        prop="machine.machine"
        width="80"
      />
      <el-table-column align="center" label="ABO" prop="ABO" width="80" />
      <el-table-column align="center" label="抗A" prop="AntiA" width="80" />
      <el-table-column align="center" label="抗B" prop="AntiB" width="80" />
      <el-table-column align="center" label="抗D" prop="AntiD" width="80" />
      <el-table-column align="center" label="Ctrl" prop="Ctrl" width="80" />
      <el-table-column align="center" label="Ac" prop="Ac" width="80" />
      <el-table-column align="center" label="Bc" prop="Bc" width="80" />
      <el-table-column align="center" label="Rh(D)" prop="RhD" width="80" />
      <el-table-column align="center" label="抗筛" prop="ABScr" width="80" />
      <el-table-column
        align="center"
        label="入库时间"
        prop="created_at"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column align="center" label="操作" width="80">
        <template #default="scope">
          <el-link
            icon="el-icon-document"
            size="mini"
            type="primary"
            @click="handleDetail(scope.row)"
          >
            详情
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :pagesize="queryForm.pagesize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <detail ref="detail" />
  </div>
</template>

<script>
  import { getAll as getList } from '@/api/inspectAbo'
  import Detail from './components/detail'
  import { removeEmptyField } from '@/utils'
  import { getAll as getMachines } from '@/api/machine'
  export default {
    name: 'InspectMachine',
    components: {
      Detail,
    },
    data() {
      return {
        list: [],
        machines: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        listLoading: false,
        queryForm: {
          machine: '',
          blood_id: '',
          created_at: '',
          page: 1,
          pagesize: 10,
        },
      }
    },
    created() {
      this.getMachines()
      this.fetchData()
    },
    methods: {
      async getMachines() {
        const {
          data: { data },
        } = await getMachines({ pagesize: 100, order: 'name asc' })
        this.machines = data
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(removeEmptyField(this.queryForm))
        this.list = data.data
        this.total = data.total
        this.listLoading = false
      },
      handleDetail(row) {
        console.log(row)
        this.$refs['detail'].show(row)
      },
      handleSizeChange(val) {
        this.queryForm.pagesize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .pr8 {
    padding-right: 8px;
  }
</style>
